home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / TTidyApplication w⁄Summa Folder / TTidyApplication w⁄Summary / For MacApp 3.0.1 / UTidyApplication.cp next >
Encoding:
Text File  |  1994-02-26  |  5.5 KB  |  221 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        UTidyApplication.cp
  3.  
  4.     Contains:    TTidyApplication::Free() and functions and member functions
  5.                 which it calls. For use with MacApp 3.0.1.
  6.  
  7.     Written by:    Adam Wildavsky
  8.  
  9.     Copyright:    © 1994 by Adam Wildavsky
  10.  
  11.     Change History (most recent first):
  12.  
  13.                  2/26/94    aw        Propagated a change from the 3.1 version: added
  14.                                      #ifdef qCleanUpAtQuit to TTidyApplication::Free().
  15.                                     Added a comment.
  16.                  2/24/94    aw        Trimmed blanks, added punctuation. No code changes.
  17.                  1/23/94    aw        Baseline.
  18.  
  19.     To Do:
  20.         Clean up non-object allocations
  21. */
  22.  
  23.  
  24. #ifndef __UTIDYAPPLICATION__
  25. #include "UTidyApplication.h"
  26. #endif
  27.  
  28. #ifndef __MACAPP__
  29. #include <MacApp.h>
  30. #endif
  31.  
  32.  
  33. #pragma segment AClose
  34. static void DeInitUMenuMgr()
  35. {
  36.     /*******
  37.  
  38.     Clean up the objects allocated by InitUMenuMgr, in reverse order of allocation.
  39.  
  40.     ********/
  41.  
  42.     extern TObject * gMenuTable;    //    Actually these are all TObject descendants,
  43.     extern TObject * gCmdTable;        //    but this will work! If we had declared them
  44.     extern TObject * gMenuIDList;    //    as their actual types we'd have cast them
  45.                                     //    to TObjects anyway for FreeIfObject().
  46.  
  47.     gMenuTable    = FreeIfObject(gMenuTable);
  48.     gCmdTable    = FreeIfObject(gCmdTable);
  49.     gMenuIDList    = FreeIfObject(gMenuIDList);
  50. }
  51.  
  52.  
  53. #pragma segment AClose
  54. static void DeInitUObject()
  55. {
  56.     /*******
  57.  
  58.     Clean up the object allocated by InitUObject
  59.  
  60.     ********/
  61.  
  62.     extern TObject * pOrderedClassIds;    //    Actually it's a TNameOrderedClassIDs, but this will work!
  63.     pOrderedClassIds = FreeIfObject(pOrderedClassIds);
  64. }
  65.  
  66.  
  67. #pragma segment AClose
  68. static void DeInitUDialog()
  69. {
  70.     /*******
  71.  
  72.     Clean up the objects allocated by InitUDialog, in reverse order of allocation.
  73.  
  74.     ********/
  75.  
  76.     //    This test is necessary because gFloatingTEManager and gParamTxt
  77.     //    are -not- initialized to NULL when they are defined in UDialog.cp
  78.     if (gUDialogInitialized)
  79.     {
  80.         gFloatingTEManager    = (TFloatingTEManager *)    FreeIfObject(gFloatingTEManager);
  81.  
  82.         extern TAssociation * gParamTxt;    //    It should be empty now
  83.         gParamTxt            = (TAssociation *)            FreeIfObject(gParamTxt);
  84.     }
  85. }
  86.  
  87.  
  88. #pragma segment AClose
  89. static void DeInitUMacApp()
  90. {
  91.     /*******
  92.  
  93.     Clean up the objects allocated by InitUMacApp, in reverse order of allocation.
  94.  
  95.     ********/
  96.  
  97.     DeInitUDialog();
  98.     DeInitUMenuMgr();
  99.  
  100.     extern TDynamicArray * gSignatures;
  101.     gSignatures            = (TDynamicArray *) FreeIfObject(gSignatures);
  102.  
  103.     if (gGlobalContext != NULL)
  104.     {
  105.         gGlobalContext->fOtherObjects->FreeAll();
  106.         gGlobalContext = (TContext *) FreeIfObject(gGlobalContext);
  107.     }
  108.  
  109.     gPrintHandler        = (TPrintHandler *)    FreeIfObject(gPrintHandler);
  110.     gNullPrintHandler    = (TPrintHandler *) FreeIfObject(gNullPrintHandler);
  111.  
  112.     DeInitUObject();
  113. }
  114.  
  115.  
  116. #pragma segment AClose
  117. static void LastChance()
  118. {
  119.     //    Set a breakpoint here in order to check for memory leaks.
  120.     //    In Jasik do an "Objects by Time".
  121.  
  122. #if qDebug
  123.     ProgramBreak("It's time to check for memory leaks!");
  124. #endif
  125. }
  126.  
  127.  
  128. #pragma segment AClose
  129. void TTidyApplication::CleanUpAfterIApplication()
  130. {
  131.     /*******
  132.  
  133.     Clean up the objects allocated by TApplication::IApplication, in reverse order of allocation.
  134.  
  135.     ********/
  136.  
  137.     gMenuBarManager        = (TMenuBarManager *)    FreeIfObject(gMenuBarManager);
  138.     gMacAppDependencies    = (TDependencySpace *)    FreeIfObject(gMacAppDependencies);
  139.  
  140.     fPendingReplyList    = (TEventList *)        FreeListIfObject(fPendingReplyList);
  141.  
  142.     //    This rigamarole is necessary because TEvent::Free() checks to see whether
  143.     //    the event is still on the event list.
  144.     if (fEventList != NULL)
  145.     {
  146.         TEvent * anEvent;
  147.  
  148.         while ((anEvent = (TEvent *) fEventList->Last()) != NULL)
  149.         {
  150.             fEventList->AtDelete(fEventList->GetSize());
  151.             anEvent->Free();
  152.         }
  153.  
  154.         fEventList->Free();
  155.     }
  156.  
  157.     fDocumentList        = (TList *)                FreeListIfObject(fDocumentList);
  158.  
  159.     //    Must do this -before- freeing fFreeWindowList
  160.     TView * theDeskScrapView = gClipboardMgr->fClipOrphanage;
  161.     if (theDeskScrapView != NULL)
  162.     {
  163.         //    Because TDeskScrapView overrides Free to -not- call inherited::Free
  164.         theDeskScrapView->TView::Free();
  165.  
  166.         if (gClipboardMgr->fClipView == theDeskScrapView)
  167.             gClipboardMgr->fClipView = NULL;
  168.         gClipboardMgr->fClipOrphanage = NULL;
  169.     }
  170.  
  171.     fFreeWindowList        = (TList *)                FreeListIfObject(fFreeWindowList);
  172. }
  173.  
  174.  
  175. #pragma segment AClose
  176. void TTidyApplication::CleanUpJustInTimeObjects()
  177. {
  178.     //    gWindows is created on a "just in time" basis by RegisterWindow() in UWindows.cp.
  179.     //    It seems to be the only object that MacApp creates in this fashion.
  180.  
  181.     extern TDynamicArray * gWindows;
  182.     gWindows = (TDynamicArray *) FreeIfObject(gWindows);
  183. }
  184.  
  185.  
  186. #pragma segment AClose
  187. pascal void TTidyApplication::Free() // Override
  188. {
  189.     /******
  190.  
  191.     Some of the objects which MacApp 3.0.1 allocates are freed by TApplication::Close(),
  192.     which is called from TQuitCommand::DoIt(). Some others are freed when CleanUpMacApp()
  193.     calls gApplication->Free(), resulting in calls to TCommandHandler::Free() and
  194.     TEventHandler::Free(). Others are not freed at all; they disappear when the
  195.     application's heap is disposed of after the application terminates.
  196.  
  197.     This set of functions endeavors to clean up what MacApp doesn't. One reason to
  198.     do so is that one can set a breakpoint just before the application terminates
  199.     and learn from the results. Any objects left are objects one's own application
  200.     has allocated but not freed. Some of these may span the life of the application
  201.     and need not be freed except to make the rest stand out (as above); others
  202.     represent likely memory leaks.
  203.  
  204.     *******/
  205.  
  206. #ifdef qCleanUpAtQuit
  207.     this->CleanUpAfterIApplication();
  208.     this->CleanUpJustInTimeObjects();
  209.  
  210.     DeInitUMacApp();
  211. #endif
  212.  
  213.     inherited::Free();
  214.  
  215.     //    Be sure not to refer to members of gApplication
  216.     //    after calling inherited::Free()!
  217.  
  218. #ifdef qCleanUpAtQuit
  219.     LastChance();
  220. #endif
  221. }